    //
//  OosReview.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/24/11.
//  Copyright 2011 11870.com. All rights reserved.
//


#import <oos4ios/Model/OosReview.h>
#import "../../Utils/OosEnum+Internal.h"

@implementation OosReview

//static NSString *fetchPredicate = @"author.slug = %@ AND service.slug = %@";
static NSString *fetchPredicate = @"userSlug = %@ AND serviceSlug = %@";

+ (OosReview *) reviewFromServiceSlug:(NSString *)serviceSlug 
						  andUserSlug:(NSString *)userSlug
{
    
    OosReview *result = [super objectWithPredicate: fetchPredicate, userSlug, serviceSlug];
    
    if (!result) {
        result = [super create];
        result.serviceSlug = serviceSlug;
        result.userSlug = userSlug;
    }
    
//	NSSet *fetched = [super objectsWithPredicate:
//					  fetchPredicate, userSlug, serviceSlug];
//	OosReview *result;
//	if ([fetched count] > 0) {
//		result = [fetched anyObject];
//	} else {
//		result = [super create];
//	}
	return result;
}	

@dynamic tags;
@dynamic lists;
@dynamic author;
@dynamic title;
@dynamic content;
@dynamic rating;
@dynamic service;
@dynamic multimedias;
@dynamic privacyString;

@dynamic userSlug;
@dynamic serviceSlug;

@dynamic tagsString;
@dynamic listsString;

- (OosPrivacy *)privacy
{
    if (mPrivacy) {
        return mPrivacy;
    }
    if (self.privacyString) {
        NSString *privacyString = self.privacyString;
        OosPrivacy *privacy = [OosPrivacy enumFromLiteral:privacyString];
        return privacy;
    }
    return nil;
}

- (void)setPrivacy:(OosPrivacy *)privacy
{
    mPrivacy = privacy;
    if (privacy) {
        self.privacyString = [[[privacy description] copy] autorelease];
    } else {
        self.privacyString = nil;
    }
}

@end
